// Author: Li Jie <cpunion@gmail.com>
// Date: 2006/05/10


// Process URL encoding data
function process_params(data){
	var result = {};
		  var params = data.split('&');
		  for (var i=0; i<params.length; ++i){
			  	  var param = params[i].split('=');
	  	  if (param.length == 0)
				  	  	  continue;
			  	  else if (params.length == 1)
				  	  	  params[1] = '';
			  	  var key   = decodeURIComponent(param[0]);
			  	  var value = decodeURIComponent(param[1]);

			  	  build_deep_hash(result, key, value);
		  }
		  return result;
}

// private method
function build_deep_hash(hash, key, value, is_array)
{
	if (key.match(/^.+\[\]$/)){
		key = key.substring(0, key.length-2);
		value = [value];
		build_deep_hash(hash, key, value, true);
	}else{
		var start = key.indexOf('[');
		var end   = key.indexOf(']');
		if (start <= 0 || (end <= start+1) || end != (key.length-1)){
			if (is_array != undefined){
				if (hash[key] == undefined)
				    hash[key] = value;
				else
			        hash[key] = hash[key].concat(value);
			} else {
			    hash[key] = value;
			}
		}else{
			var newkey = key.substring(0, start);
			var subkey = key.substring(start+1, key.length-1);
			if (hash[newkey] == undefined)
				hash[newkey] = {};
			build_deep_hash(hash[newkey], subkey, value);
		}
	}
}

// Capitalize
String.prototype.capitalize = function(){
  if (this.length == 0)
    return "";
  return this.charAt(0).toUpperCase() + this.substr(1);
}


// Page object, like RJS
Page = {
  replace_html: function(id, content){
    Element.update(id, content);
  },

  replace: function(id, content){
    Element.update(id, content);
  },
   
  alert: function(message){
    alert(message);
  },
   
  delay: function(seconds, func){
    setTimeout(func, seconds * 1000);
  },

  draggable: function(id, options){
    new Draggable(id, options);
  },

  drop_receiving: function(id, drop_func){
    Droppables.add(id, {onDrop: drop_func})
  },

  hide: function(){
    for (var i=0; i<arguments.length; ++i){
      Element.hide(arguments[i]);
	}
  },

  insert_html: function(position, id, content){
	var insertion = eval("Insertion." + position.capitalize());
	new insertion(id, content);
  },

  redirect_to: function(url){
    window.location.href = url;
  },
  
  remove: function(){
    for (var i=0; i<arguments.length; ++i){
      Element.remove(arguments[i]);
	}
  },

  select: function(pattern){
	return $$(pattern);
  },

  show: function(){
    for (var i=0; i<arguments.length; ++i){
      Element.show(arguments[i]);
	}
  },

  sortable: function(id, update_func){
    Sortable.create(id, {
	  onUpdate: function(){
		update_func(process_params(Sortable.serialize(id)));
      }
	});
  },

  toggle: function(){
    for (var i=0; i<arguments.length; ++i){
      Element.toggle(arguments[i]);
	}
  },

  visual_effect: function(name, id, js_options){
	var TOGGLE_EFFECTS = ['toggle_appear', 'toggle_slide', 'toggle_blind'];
    if (TOGGLE_EFFECTS.indexOf(name) != -1)
		Effect.toggle(id, name.replace(/^toggle_/,''), js_options);
	else {
		var effect = eval("Effect." + name.camelize().capitalize());
	    new effect(id, js_options);
	}
  }
}

